package main

import (
	"bufio"
	"fmt"
	"net"
)

/*
   Author: mahaiyuan
   Date: 2021/6/3 8:33 下午
   Description:
*/
const resData = "{\"errNo\": 0, \"errStr\": \"success\"}"
func main() {
	listen, err := net.Listen("tcp", "127.0.0.1:8888")
	if err != nil {
		panic(fmt.Sprintf("listen failed %v", err))
	}
	for true {
		conn, err := listen.Accept() //创建连接
		if err != nil {
			fmt.Println("accept error. ", err)
			continue
		}
		go process(conn)
	}
}

func process(conn net.Conn) {
	defer conn.Close()	//关闭连接
	for true {
		reader := bufio.NewReader(conn)
		data, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println("read data error.",err)
			break
		}
		fmt.Println("服务器收到数据:", data)
		// 向客户端响应
		conn.Write([]byte(resData))
	}
}